if (args.length < 3) {
    println "Usage: "+getClass().getName()+" <package-metadata-directory> <output-dir> <log-file-path> <package-list-filter-file-path>"
    return
}

package_dir = args[0]
output_dir = args[1]
log = new File(args[2])

log.append("\n== Converting diff packages metadata to csv ==\n")
packages_out = new File("$output_dir/packages.csv")
package_files_out = new File("$output_dir/package-files.csv")
package_dependencies_out = new File("$output_dir/package-dependencies.csv")

packages_out.setText("")
package_dependencies_out.setText("")
package_files_out.setText("")
    
metadata_files = new TreeSet()
if (args.length == 4) {
    //keep only the packages that were added recently
    added_packages_file = args[3]
    added_packages_list = new File(added_packages_file).text
    added_packages = added_packages_list.split()
    for (added_package in added_packages) {
        metadatafile = new File(package_dir +"/"+added_package+"/metadata.xml")
        if (metadatafile.exists())
            metadata_files.add(metadatafile.absolutePath)
    }
} else {
    for (dirs in (new File(package_dir).listFiles())) {
        println dirs
        if (dirs.isDirectory()) {
            metadatafile = new File(dirs.absolutePath+"/metadata.xml")
            if (metadatafile.exists())
                metadata_files.add(metadatafile.absolutePath)
        }
    }
}

for (file in metadata_files) {
    f = new File(file)
    log.append("\n=== XML to CSV: "+f.getParentFile().getName())
    try {
        parse_package_metadata(f, packages_out, package_files_out, package_dependencies_out)
    } catch (Exception e) {
        log.append("\n*** Exception: "+e.getMessage())
        e.printStackTrace()
    }
}

def parse_package_metadata(File metadata, File packages_out, File package_files_out, File package_dependencies_out) throws Exception {

    entries     = new XmlParser().parseText(metadata.text)
    pkg         = entries.get("package")[0]
    pkgfilename    = pkg.'@filename'
    name        = pkg.name[0].text()
    license     = pkg.license[0].text()
    home        = pkg.homepage[0].text()
    description = pkg.description[0].text().replaceAll("\"","\\\\\"")
    scripts     = pkg.scripts[0]
    prein       = scripts.prein[0].text().replaceAll("\"","\\\\\"")
    postin      = scripts.postin[0].text().replaceAll("\"","\\\\\"")
    preun       = scripts.preun[0].text().replaceAll("\"","\\\\\"")
    postun      = scripts.postun[0].text().replaceAll("\"","\\\\\"")
    distrib     = pkg.distribution[0].text()
    version     = pkg.version[0].text()
    release     = pkg.release[0].text()
    branch      = pkg.branch[0].text()
    section     = pkg.section[0].text()
    architecture = pkg.arch[0].text()
    group       = pkg.group[0].text()
    sourcerpm   = pkg.sourcerpm[0].text()
    buildhost   = pkg.buildhost[0].text()
    buildtime   = pkg.buildtime[0].text()
    size        = pkg.size[0].text()
    packager    = pkg.packager[0].text()
    summary     = pkg.summary[0].text()
    changelog   = pkg.changelog[0].text().replaceAll("\"","\\\\\"")
    idx = packager.indexOf("<")
    if (idx >=0 && packager.length()>1)
        email = packager.substring(idx+1, packager.length()-1)
    else
        email = ""

    packages_out.append("\"$distrib\",\"$pkgfilename\",\"$version\",\"$release\",\"$branch\",\"$section\",\"$email\",\"$name\",\"$home\",\"$license\",\"$group\",\"$summary\",\"$description\",\"$changelog\",\"$sourcerpm\",\"$packager\",\"$prein\",\"$postin\",\"$preun\",\"$postun\",\"$architecture\",\"$buildhost\",\"$buildtime\",\"\",\"\",\"$size\",\"\",NULL\n")

    for (provide in pkg.provides) {
           item     = provide.'@name'
           dep      = "provides"
           package_dependencies_out.append("\"$pkgfilename\",\"$dep\",\"$item\",\"\"\n")
    }
    for (require in pkg.requires) {
            item    = require.'@name'
            dep     = "requires"
            package_dependencies_out.append("\"$pkgfilename\",\"$dep\",\"$item\",\"\"\n")
    }
    for (conflict in pkg.conflicts) {
           item    = conflict.'@name'
           version = conflict.'@version'
           dep     = "conflicts"
           package_dependencies_out.append("\"$pkgfilename\",\"$dep\",\"$item\",\"$version\"\n")
    }


    for (pkgfile in pkg.file) {
        path    = pkgfile.'@path'
        type    = pkgfile.'@type'
        size    = pkgfile.'@size'
        extra   = pkgfile.'@extra'
        cat   = pkgfile.'@cat'
        if (extra == null)
            extra = 0;
        if (cat == null)
            cat = 'package';
        package_files_out.append("\"${pkgfilename}\",\"${path}\",\"${type}\",\"${size}\",\"${extra}\",\"${cat}\"\n")
    }
    
    return
}

